//
//  QCLowController.swift
//  爱汽车
//
//  Created by tucici on 2023/8/27.
//

import UIKit

class QCLowController: QCViewController {
    var prepos : PrePos!
    var array = ["AIROUT","自定义"]
    var values = [false,false]
    var index = 100
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("LOW")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCSettingCell.nib(), forCellReuseIdentifier: QCSettingCell.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override var bytes: [UInt8]{
        didSet{
            let ack = bytes[7]
            if ack == UInt8(AIQC.userInfo.ack){
                let data = [UInt8](bytes[AI_index..<bytes.count])
                values = [Int(data[90]) == 0, Int(data[90]) == 1]
                tableView.reloadData()
            }
        }
    }
}
//MARK: - UITableViewDataSource
extension QCLowController:UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCSettingCell.ID()) as! QCSettingCell
        cell.title.text = LS(array[indexPath.row])
        cell.type = indexPath.row == 0 ? .switchBtn(values[indexPath.row]) : .onlyText("")
        cell.block = {[weak self] ison in
            BLEData.appSetDataToPer(AIQC.oneClickLowPosition.cmd, ison ? "00" : "01").write
            self?.values[indexPath.row] = ison
            tableView.reloadData()
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCLowController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0{return}
        if values[0] == true {return}
        let vc = QCCustomController()
        vc.prepos = prepos
        push(vc,true)
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
